package com.lej.my.settings

import android.view.View
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import com.lej.core.base.BaseFragment
import com.lej.core.common.PCacheInfo
import com.lej.core.router.CRouter
import com.lej.core.utils.extention.addClearFlag
import com.lej.core.utils.extention.router
import com.lej.core.utils.extention.toast
import com.lej.dataservice.club.http.bean.ResStatus
import com.lej.my.BR
import com.lej.my.R
import com.lej.my.databinding.MyFragmentSettingsBinding
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.util.XPopupUtils
import com.qmuiteam.qmui.kotlin.onClick

/**
 *  author: cjq
 *  date  : 2022/6/9 0009
 *  desc  :
 */
@Route(path = CRouter.mySettings)
class MySettingsFragment : BaseFragment<MyFragmentSettingsBinding, MySettingsViewModel>() {
    override fun getLayout(): Int = R.layout.my_fragment_settings

    override fun initVariableId(): Int = BR.vm

    override fun initData() {
        binding.ivPushSwitch.isSelected = PCacheInfo.pushClose
        binding.llPushSwitch.onClick {
            PCacheInfo.pushClose = !PCacheInfo.pushClose
            binding.ivPushSwitch.isSelected = PCacheInfo.pushClose
        }
        binding.btAccountDispose.onClick {
            activity?.let {
                XPopup.Builder(it)
                    .dismissOnBackPressed(false)
                    .hasStatusBar(false)
                    .hasBlurBg(true)
                    .dismissOnTouchOutside(false)
                    .asCustom(object : CenterPopupView(it) {
                        override fun getImplLayoutId() =
                            R.layout.my_fragment_settings_loginoff_dialog

                        override fun initPopupContent() {
                            super.initPopupContent()
                            findViewById<View>(R.id.cl_root).onClick { dismiss() }
                            findViewById<View>(R.id.btn_negative).onClick {
                                dismiss()
                                disposeAccount()
                            }
                            findViewById<TextView>(R.id.btn_positive)?.onClick { dismiss() }
                        }

                        override fun getMaxWidth() = XPopupUtils.getWindowWidth(context)
                    })
                    .show()
            }
        }
    }

    override fun initViewObservable() {
    }

    private fun disposeAccount() {
        viewModel.disposeAccount().addObserver {
            when (it) {
                is ResStatus.Success -> {
                    "帐号销毁成功".toast()
                    router(CRouter.login) { addClearFlag() }
                }
                is ResStatus.Error -> {
                    it.error.message.toast()
                    hideLoading()
                }
                is ResStatus.Loading -> showLoading()
            }
        }
    }
}